<?php
/**
 * 描述
 * MenuController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author Huangdaohai <huangdaohai.2007@163.com>
 * @version $Id$
 * @access public
 */
class MenuController extends AdminController
{
	/**
	 * 菜单列表
	 *
	 */
	public function actionIndex($fid=0) 
	{
		$menuData = AdminMenu::getMenuData($this->module->id);
		$tree = new Tree($menuData);
		$str="<tr id='list\$id'>
				<td><input type='checkbox' value='\$id' name='ids[]' class='ids'> \$id</td>
				<td>\$spacer\$menu_name</td>
				<td>\$display</td>
				<td>\$str_manage</td>
			</tr>";
		$categorys = $tree->get_tree(0, $str);
		$this->render('index',array('categorys'=>$categorys));
	}
	
	
	//菜单添加 
	public function actionCreate($fid='')
	{
		$model = new AdminMenu();
		if(isset($_POST['ajax']) && $_POST['ajax']==='menu-_form-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminMenu'])){
			$model->attributes=$_POST['AdminMenu'];
			if($model->save()){
				if($model->fid!=0){
					$parent = AdminMenu::model()->findByPk($model->fid);
					$pids = $parent->pids.$model->menu_id.',';
				}else{
					$pids = ','.$model->menu_id.',';
				}
				AdminMenu::model()->updateByPk($model->menu_id, array('pids'=>$pids));
				
				AdminLog::add('添加菜单'.$model->menu_name);
				$alert="SendWebMsg('success','菜单添加成功','".$this->createUrl('menu/index#list'.$model->menu_id)."')";
			}else{
				if($model->hasErrors()){
					$alert="SendWebMsg('cue','请修正页面错误提示项')";
				}else{
					$alert="SendWebMsg('error','你的网络不给力，操作失败')";
				}
			}
			Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
		}
		$this->render('_form',array('model'=>$model,'fid'=>$fid));
	}
	//菜单编辑
	public function actionUpdate($id){
        $model = AdminMenu::model()->findByPk((int)$id);
        if(isset($_POST['ajax']) && $_POST['ajax']==='menu-_form-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if(isset($_POST['AdminMenu'])){
            $model->attributes=$_POST['AdminMenu'];
            if($model->save()){
            	if($model->fid!=0){
            		$parent = AdminMenu::model()->findByPk($model->fid);
            		$pids = $parent->pids.$model->menu_id.',';
            	}else{
            		$pids = ','.$model->menu_id.',';
            	}
            	AdminMenu::model()->updateByPk($model->menu_id, array('pids'=>$pids));
            
            	AdminLog::add('编辑菜单'.$model->menu_name);
            	$alert="SendWebMsg('success','菜单编辑成功','".$this->createUrl('menu/index#list'.$model->menu_id)."')";
            }else{
            	if($model->hasErrors()){
            		$alert="SendWebMsg('cue','请修正页面错误提示项')";
            	}else{
            		$alert="SendWebMsg('error','你的网络不给力，操作失败')";
            	}
            }
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        $this->render('_form',array('model'=>$model,'fid'=>$model->fid));
    }
	
	/**
	 * 菜单删除 
	 *
	 */
	public function actionDelete($id)
	{
		$result=AdminMenu::model()->findByPk($id);        
        //删除所有相关的栏目
		if(AdminMenu::model()->deleteAll("FIND_IN_SET('$id' ,pids)")){
			AdminLog::add('删除菜单'.$result->menu_name);            
	        //$this->showMessage(1,'删除成功',);
			$this->ajaxReturn(0,'删除成功',$this->createUrl('menu/index'));
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}